# -*-  conding: utf-8 -*-
# Author: czh
# Datetime : 2022/5/9 16:32

import tushare as ts
import time
# from datetime import datetime
import datetime
import tkinter as tk
import requests
import pysnowball as ball

# 获取所有股票当日数据
# stock = ts.get_today_all()
# # 给数据添加一个日期
# stock['date'] = datetime.date.today()
# # stock.to_csv('./stock_data.csv', encoding='utf-8', index=None)
# print(stock)


pro = ts.pro_api('cf37ab059637ac7f5d628bdd046a9bcb2c1e34c2fa7546e6b8659225')


# market: sh:沪市 sz:深市
# number: 股票代码
def getData(market, number):
    url = 'http://hq.sinajs.cn/list=' + market + number
    headers = {"Referer": "http://finance.sina.com.cn", "Connection": "close"}
    respond = requests.get(url, headers=headers)
    index = respond.text.find('=') + 2
    data_arr = respond.text[index:-4].split(',')
    # data = {'股票代码': data_arr[0], '今日开盘价': data_arr[1], '昨日收盘价': data_arr[2], '当前价格': data_arr[3], '今日最高价': data_arr[4],
    #         '今日最低价': data_arr[5], '竞买价': data_arr[6], '竞卖价': data_arr[7], '成交的股票数': data_arr[8], '成交金额': data_arr[9],
    #         '买一挂单': data_arr[10], '买一报价': data_arr[11], '买二挂单': data_arr[12], '买二报价': data_arr[13],
    #         '买三挂单': data_arr[14], '买三报价': data_arr[15], '买四挂单': data_arr[16], '买四报价': data_arr[17],
    #         '买五挂单': data_arr[18], '买五报价': data_arr[19], '卖一挂单': data_arr[20], '卖一报价': data_arr[21],
    #         '卖二挂单': data_arr[22], '卖二报价': data_arr[23], '卖三挂单': data_arr[24], '卖三报价': data_arr[25],
    #         '卖四挂单': data_arr[26], '卖四报价': data_arr[27], '卖五挂单': data_arr[28], '卖五报价': data_arr[29],
    #         '日期': data_arr[30], '时间': data_arr[31]}
    # print(data)
    open = float(data_arr[1])
    close = float(data_arr[3])
    low = float(data_arr[5])
    shiti = max(open, close) - min(open, close)
    xiaying = min(open, close) - low
    # if low * 1.06 < min(open, close):
    #     print(low * 1.06, min(open, close), trade_date)
    #     pct_chg = get_next_date(ts_code, trade_date)
    if shiti == 0.0 or xiaying == 0.0:
        # print("跳过：", shiti, xiaying)
        pass
    else:
        jieguo = round(xiaying / shiti, 2)

        if jieguo > 2.5:
            print("股票名称:", data_arr[0], "股票代码:", number, jieguo)
            # print(open, high, low, close, trade_date, ts_code)
            return data_arr[0]

    # return data


if __name__ == '__main__':
    df = pro.query('stock_basic', exchange='', list_status='L', fields='ts_code,name')
    # for idx, data in df.iterrows():
    #     marker = data[0][-2:].lower()
    #     number = data[0][:6]
    #     time.sleep(0.2)
    #     realTimeData = getData(marker, number)


    # start_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    # print(start_time)
    # df = pro.query('stock_basic', exchange='', list_status='L', fields='ts_code,name')
    # for idx, data in df.iterrows():
    #     code = data[0][-2:] + data[0][:6]
    #     res = ball.quotec("SZ000998,SZ000997")
    #     print(code, res["data"][0]["current"])
    # end_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    # print(end_time)
    # print(realTimeData)
    # realTimeData = getData("sz", "000998")

    #     res = ball.quotec("SZ000998,SZ000997")
    #     print(code, res["data"][0]["current"])
# 股票数据获取
# def get_news():
#     p_list = []
#     # 300671 富满电子
#     # 600460 士兰微
#     # 300777 国民技术
#     ### 每次麻烦的就是需要在下面更新股票代码
#     news = ts.get_realtime_quotes('300077')
#     # 昨日收盘价
#     start_pri = news["open"][0]
#     # 今日开盘价
#     yes_pri = news["pre_close"][0]
#     # 现价 实时
#     now_pri = news["price"][0]
#     print(now_pri)
#     # 涨跌幅度
#     up_num = round(((float(now_pri) - float(yes_pri)) / float(yes_pri)), 4) * 100
#     # up_num = round(up_num, 4)*100
#     if up_num > 0:
#         up_num = "+" + str(up_num)
#     else:
#         up_num = str(up_num)
#     p_list.append(datetime.now().strftime('%H:%M:%S'))
#     p_list.append(news["price"][0])
#     p_list.append(up_num)
#
#     return p_list

#
# # 数据刷新的回调
# def refreshDate():
#     # 获取数据
#     p_list = get_news()
#     # 分别使用上面获取的数据, 填充text
#     text1.delete(0.0, tk.END)
#     text1.insert(tk.INSERT, p_list[0])
#     text1.update()
#
#     text2.delete(0.0, tk.END)
#     text2.insert(tk.INSERT, p_list[1])
#     text2.update()
#
#     text3.delete(0.0, tk.END)
#     text3.insert(tk.INSERT, p_list[2])
#     text3.update()
#
#     windows.after(1000, refreshDate)
#
#
# # 显示价格
# def show_price():
#     # 定义全局变量, 目的是在refreshText 中可以调用这几个变量
#     global text1
#     global text2
#     global text3
#     global windows
#
#     # 创建基础窗口
#     windows = tk.Tk()
#     windows.geometry('130x140+1775+680')  ## 规定窗口大小500*500像素
#     windows.resizable(False, False)  ## 规定窗口不可缩放
#
#     # 定义组件
#     label1 = tk.Label(windows, text="N_T:")
#     label1.grid(row=0, column=1, padx=2, pady=2)
#
#     text1 = tk.Text(windows, width=8, height=1)
#     text1.grid(row=0, column=2, padx=10, pady=10)
#
#     label2 = tk.Label(windows, text="N_P:")
#     label2.grid(row=1, column=1, padx=2, pady=2)
#
#     text2 = tk.Text(windows, width=8, height=1)
#     text2.grid(row=1, column=2, padx=10, pady=10)
#
#     label3 = tk.Label(windows, text="U_P:")
#     label3.grid(row=2, column=1, padx=2, pady=2)
#
#     text3 = tk.Text(windows, width=8, height=1)
#     text3.grid(row=2, column=2, padx=10, pady=10)
#
#     windows.after(1000, refreshDate)
#     windows.wm_attributes('-topmost', 1)
#     windows.mainloop()


# if __name__ == '__main__':
#     get_news()
